package com.redsoil.house.widget.actionbar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.redsoil.house.R;
import com.zhy.autolayout.AutoRelativeLayout;

/**
 * Created by weidingqiang on 2018/12/19.
 */

public class MatterTopBar extends AutoRelativeLayout implements View.OnClickListener{

    private ImageView img_topbar_left;

    private TextView cancel_tv;

    private TextView edit_tv;

    private TextView select_all_tv;

    private MatterTopBarImp matterTopBarImp;

    private Boolean isShowEdit = true;

    public void setMatterTopBarImp(MatterTopBarImp matterTopBarImp) {
        this.matterTopBarImp = matterTopBarImp;
    }

    public MatterTopBar(Context context) {
        super(context);
    }

    public MatterTopBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.matter_topbar, this, true);

        img_topbar_left = (ImageView) this.findViewById(R.id.img_topbar_left);
        cancel_tv = (TextView) this.findViewById(R.id.cancel_tv);
        edit_tv = (TextView) this.findViewById(R.id.edit_tv);
        select_all_tv = (TextView) this.findViewById(R.id.select_all_tv);

        img_topbar_left.setOnClickListener(this);
        cancel_tv.setOnClickListener(this);
        edit_tv.setOnClickListener(this);
        select_all_tv.setOnClickListener(this);

        showEdit();
    }

    @Override
    public void onClick(View v) {

        if(matterTopBarImp == null)
            return;

        switch (v.getId()){
            case R.id.img_topbar_left:
                matterTopBarImp.back();
                break;
            case R.id.cancel_tv:
                isShowEdit = true;
                matterTopBarImp.cancel();
                break;
            case R.id.edit_tv:
                isShowEdit = false;
                matterTopBarImp.edit();
                break;
            case R.id.select_all_tv:
                matterTopBarImp.selectAll();
                break;
        }
        showEdit();
    }

    private void showEdit(){
        if(isShowEdit){
            img_topbar_left.setVisibility(VISIBLE);
            edit_tv.setVisibility(VISIBLE);

            cancel_tv.setVisibility(GONE);
            select_all_tv.setVisibility(GONE);
        }else {
            img_topbar_left.setVisibility(GONE);
            edit_tv.setVisibility(GONE);

            cancel_tv.setVisibility(VISIBLE);
            select_all_tv.setVisibility(VISIBLE);
        }
    }

    public void clear(){
        isShowEdit = true;
        showEdit();
    }


}
